package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.entity.ExpProjectReq;
import com.softer.wxzj.entity.MaintainObjectSearch;
import com.softer.wxzj.entity.MaintenanceObject;
import com.softer.wxzj.mapper.MaintainObjectSearchMapper;
import com.softer.wxzj.mapper.MaintenanceObjectMapper;
import com.softer.wxzj.service.MaintainObjectSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/8/9
 */
@Service
public class MaintainObjectSearchServiceImpl implements MaintainObjectSearchService {
    @Autowired
    private MaintainObjectSearchMapper maintainObjectSearchMapper;
    @Autowired
    private MaintenanceObjectMapper maintenanceObjectMapper;

    /**
     * 根据维修对象ID查询维修项目列表
     *
     * @param page          分页实体
     * @param expProjectReq 查询实体
     * @return 维修项目列表
     */
    @Override
    public PageInfo<ExpProjectReq> getProjectListByMaintenanceObjectId(Page page, ExpProjectReq expProjectReq) {
        List<ExpProjectReq> list;
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (StringUtils.isNotEmpty(expProjectReq.getFuzzy())) {
            //模糊查询
            list = maintainObjectSearchMapper.fuzzyProjectList(expProjectReq);
        } else {
            //精确查询
            list = maintainObjectSearchMapper.getProjectList(expProjectReq);
        }
        PageInfo<ExpProjectReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 维修对象查询列表
     *
     * @param page                 分页实体
     * @param maintainObjectSearch 查询实体
     * @return 维修对象查询列表
     */
    @Override
    public PageInfo<MaintainObjectSearch> getList(Page page, MaintainObjectSearch maintainObjectSearch) {
        List<MaintainObjectSearch> list;
        List<MaintainObjectSearch> listAll;
        MaintainObjectSearch pageMain = new MaintainObjectSearch();
        MaintainObjectSearch allMain = new MaintainObjectSearch();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (StringUtils.isNotEmpty(maintainObjectSearch.getFuzzy())) {
            //模糊查询
            list = maintainObjectSearchMapper.fuzzyList(maintainObjectSearch);
        } else {
            //精确查询
            list = maintainObjectSearchMapper.getList(maintainObjectSearch);
        }
        list.forEach(x -> {
            String name = getMaintenanceObject(x.getMaintenanceObjectId());
            x.setMaintenanceObjectId(name);
            //查询项目数量和预算总金额
            MaintainObjectSearch objectSearch = maintainObjectSearchMapper.getProjectNum(x.getId());
            x.setProjectNum(objectSearch.getProjectNum());
            x.setMoney(objectSearch.getMoney());
            // 查询收益人数
            x.setOwnerNum(maintainObjectSearchMapper.getOwnerNum(x.getId()));
            if (null == pageMain.getProjectNum()) {
                pageMain.setProjectNum(0);
            }
            pageMain.setProjectNum(pageMain.getProjectNum() + x.getProjectNum());
            if (null == pageMain.getMoney()) {
                pageMain.setMoney(BigDecimal.ZERO);
            }
            if (null != x.getMoney()) {
                pageMain.setMoney(pageMain.getMoney().add(x.getMoney()));
            }
        });
        if (StringUtils.isNotEmpty(maintainObjectSearch.getFuzzy())) {
            //模糊查询
            listAll = maintainObjectSearchMapper.fuzzyList(maintainObjectSearch);
        } else {
            //精确查询
            listAll = maintainObjectSearchMapper.getList(maintainObjectSearch);
        }
        listAll.forEach(x -> {
            //查询项目数量和预算总金额
            MaintainObjectSearch objectSearch = maintainObjectSearchMapper.getProjectNum(x.getId());
            x.setProjectNum(objectSearch.getProjectNum());
            x.setMoney(objectSearch.getMoney());
            if (null == allMain.getProjectNum()) {
                allMain.setProjectNum(0);
            }
            allMain.setProjectNum(allMain.getProjectNum() + x.getProjectNum());
            if (null == allMain.getMoney()) {
                allMain.setMoney(BigDecimal.ZERO);
            }
            if (null != x.getMoney()) {
                allMain.setMoney(allMain.getMoney().add(x.getMoney()));
            }
        });
        list.add(pageMain);
        list.add(allMain);
        PageInfo<MaintainObjectSearch> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 获取设备名称
     *
     * @param maintenanceObjectId 设备类别ID
     * @return 设备名称
     */
    private String getMaintenanceObject(String maintenanceObjectId) {
        List<String> list = new ArrayList<>(6);
        if (StringUtils.isNotEmpty(maintenanceObjectId)) {
            MaintenanceObject maintenanceObject = maintenanceObjectMapper.selectByPrimaryKey(maintenanceObjectId);
            if (maintenanceObject != null) {
                list.add(maintenanceObject.getName());
                while (!String.valueOf(NumberEnum.ZERO.getNum()).equals(maintenanceObject.getParentId())) {
                    maintenanceObject = maintenanceObjectMapper.selectByPrimaryKey(maintenanceObject.getParentId());
                    list.add(0, maintenanceObject.getName());
                }
            }
        }
        String name = String.join(">", list);
        return name;
    }
}
